import 'dart:convert';

import 'package:jyofservice/util/webconfig.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_spinkit/flutter_spinkit.dart';
import 'package:jyofservice/util/AesUtil.dart';
import 'package:jyofservice/bean/jpush_msg_bean_entity.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:dio/dio.dart';
import 'package:fluttertoast/fluttertoast.dart';


class JpushMsg extends StatefulWidget {
  @override
  _JpushMsgState createState() => _JpushMsgState();
}

class _JpushMsgState extends State<JpushMsg> {
  late JpushMsgBeanEntity jpushMsgBeanEntity;
  bool isloading = false;
  List<JpushMsgBeanData> list = [];
  ScrollController scrollController = ScrollController();
  int page = 1;


  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    list.clear();
    getJpushMsgData();

    scrollController.addListener(() {
      if (scrollController.position.pixels ==
          scrollController.position.maxScrollExtent) {
        print('滑动到了最底部');
        page++;
        getJpushMsgData();
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('消息推送记录',
            style: TextStyle(
                color: Colors.white, fontSize: ScreenUtil().setSp(45))),
        centerTitle: true,
      ),
      body: Stack(
        alignment: Alignment(0.5, 0.5),
        children: <Widget>[
          Container(
            color: const Color(0xFFF1F1F1),
            child: ListView.builder(
              itemBuilder: (BuildContext context, int index) {
                return _buildData(index);
              },
              itemCount: list.length,
              physics: AlwaysScrollableScrollPhysics(),
              controller: scrollController,
            ),
          ),
          Offstage(
            offstage: isloading,
            child: SpinKitFadingCircle(
              color: Colors.blue,
              size: 50.0,
            ),
          )
        ],
      ),
    );
  }

  Widget _buildData(int index) {
    return Stack(
        alignment: Alignment(0.9, -0.9),
        children: <Widget>[
          Container(
            margin: EdgeInsets.only(top: 11),
            color: Colors.white,
            width: ScreenUtil().screenWidth,
            height: ScreenUtil().setHeight(260),
            child: Column(
              children: <Widget>[
                //创建时间
                Container(
                  alignment: Alignment.topLeft,
                  padding: EdgeInsets.fromLTRB(10, 0, 56, 5),
                  child: Text(
                    list[index].title!,
                    style: TextStyle(color: Colors.blue,fontSize: ScreenUtil().setSp(35)),
                    overflow: TextOverflow.ellipsis,
                  ),
                ),
                Container(
                  alignment: Alignment.topLeft,
                  padding: EdgeInsets.fromLTRB(10, 5, 0, 0),
                  child: Text(
                    list[index].createDate!,
                    style: TextStyle(color: Color(0xff666666)),
                  ),
                ),
                //标题

                //分割线
                Container(
                  alignment: Alignment.topLeft,
                  color: Color(0xffdddddd),
                  height: 1.0,
                ),
                Expanded(
                  child: Padding(
                    padding: EdgeInsets.fromLTRB(10, 0, 0, 10),
                    child: Text(
                      list[index].content!,
                      style: TextStyle(
                          color: Colors.black,
                          ),
                    ),
                  ),
                )
              ],
            ),
          ),
          Image.asset(
            'images/22.png',
            width: ScreenUtil().setWidth(70),
          ),
        ],
      );
  }

  Future getJpushMsgData() async {
    SharedPreferences sharedPreferences = await SharedPreferences.getInstance();
    String key = sharedPreferences.getString("key")!;
    String id = await AesUtil.decodeBase64(key);
    String aesData= await AesUtil.encodeBase64();
    var data = {
      "key": key,
      "view": "GD_tuisong_history",
      "wherestr": "User_id="+id,
      "limit": "10",
      "page": page,
      "sortName": "CreateDate",
      "sortOrder": "desc",
      "ClientType":"phone","ClientKey":aesData
    };
    Response response = await Dio().post(
        WebConfig().baseUrl + "DB/postrowdataforlayui",
        data: data);
    isloading = true;
    if (response.data["code"] == 0) {
      jpushMsgBeanEntity = JpushMsgBeanEntity.fromJson(response.data);
      list.addAll(jpushMsgBeanEntity.data!);


    } else {
      Fluttertoast.showToast(msg: "当前请求数据错误请重新刷新");
    }
    isloading = true;
    if (mounted) {
      setState(() {

      });
    }
  }
}
